package org.advanced.extend;

import java.util.concurrent.TimeUnit;

/**
 * @author John.zhang
 * @version Id: Stop, v 1.0 2020/12/7 16:22 ZhiYong Exp $
 * 共享变量业务层面的中断
 */
public class Stop {

    static volatile boolean stop = false;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new StopThread());
        thread.start();
        TimeUnit.SECONDS.sleep(2);
        stop = true;
        //thread.stop();//kill -9
    }

    static class StopThread implements Runnable {

        @Override
        public void run() {
            while (!stop) {
                System.out.println("running");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
